
#include "engine.h"
#include "core.h"
#include "process.h"
#include "XML\XMLTreeNode.h"
#include "GraphicsManager.h"
#include "FontManager.h"
#include "InputManager.h"
#include "Camera.h"
#include "Base.h"


//CEngine::CEngine()
//{
//	m_pCore = NULL;
//	m_pProcess = NULL;
//}

CEngine::~CEngine()
{
	Deinit();
}

void CEngine::Init(HWND hWnd)
{
	m_pLogger=new CLogger();
	m_pCore = new CCore();
	m_pCore->Init(hWnd, m_sEngineParams);
	m_pProcess->Init();
}



void CEngine::Deinit()
{
	m_pCore->Deinit();
	m_pProcess->Deinit();
}

void CEngine::Update()
{	
	
	m_pTimer.Update();
	m_pCore->Update();
	m_pProcess->Update(m_pTimer.GetElapsedTime(),m_pTimer.GetFPS());
	
}

void CEngine::Render()
{
	CGraphicsManager* gm = m_pCore->GetGraphicsManager();
	CFontManager* fm= m_pCore->GetFontManager();
	CCamera* cam=m_pProcess->GetCamera();

	gm->BeginRendering();    //Setear los estados de pintado

	//ToDo
	//CCamera* camera = m_pProcess->GetCamera();
	//gm->SetupMatrices(camera
	gm->SetupMatrices(cam);  // Configurar las c'amaras de visualizaci'on
	{
		RenderScene();
	}
	gm->EndRendering();
	
	
}

void CEngine::RenderScene()
{
	CInputManager* im=CORE->GetInputManager();
	CGraphicsManager* gm=CORE->GetGraphicsManager();
	CFontManager* fm=CORE->GetFontManager();
	//Render 3D
	m_pCore->Render();
	m_pProcess->RenderScene(); //cambiar Render por RenderScene 

	//Render 2D
	uint32 l_yaccum;
	if(im->IsUpDown(IDV_KEYBOARD,KEY_I)) m_bRenderDebugInfo=!m_bRenderDebugInfo;
	CORE->GetGraphicsManager()->DisableZBuffering();
	CORE->GetGraphicsManager()->EnableAlphaBlend();
	{
		CColor black=colBLACK;
		black.SetAlpha(0.3f);
		m_pProcess->RenderScene2D(); //cambiar Render por RenderScene
		//m_pCore->GetGUIManager()->Render();
		if(m_bRenderDebugInfo)
		{
			gm->EnableAlphaBlend();
			l_yaccum=m_pProcess->RenderDebugInfo();
			gm->DrawRectangle2D(Vect2i(0,0),200,l_yaccum+10,black,0,0,black);
			m_pProcess->RenderDebugInfo();
			gm->DisableAlphaBlend();
		}
		else
		{
			gm->EnableAlphaBlend();
			gm->DrawRectangle2D(Vect2i(0,0),320,20,black,0,0,black);
			fm->DrawDefaultText(0,0,colWHITE,"Press I key to show render debug info. FPS: %.2f", m_pTimer.GetFPS());
			gm->DisableAlphaBlend();
		}
		



	}
	CORE->GetGraphicsManager()->DisableAlphaBlend();
	CORE->GetGraphicsManager()->EnableZBuffering();


	
}

void CEngine::setProcess(CProcess* pProc)
{
	m_pProcess = pProc;
}

int CEngine::loadXML(const std::string& path)
{
	CXMLTreeNode newFile;
	

	if (!newFile.LoadFile(path.c_str()))
	{
		MessageBox(HWND_DESKTOP, "XML Not Found", "XML Not Found", 0);
		//printf("ERROR loading the file.");
		return 1;
	}

	CXMLTreeNode  mParent = newFile["Config"];
	if (mParent.Exists())
	{
		
		CXMLTreeNode m=newFile["WindowSettings"];

		if(m.Exists())
		{
			int count_aux = m.GetNumChildren();
			for (int i = 0; i < count_aux; ++i)
			{
				m_sEngineParams.m_WindowsPostion	= m(i).GetVect2iProperty("position",Vect2i(0,0));//Vect2i(m(i).GetIntProperty("pos_x"),m(i).GetIntProperty("pos_y"));
				m_sEngineParams.m_ScreenResolution	= m(i).GetVect2iProperty("resolution", Vect2i(800,600));
				m_sEngineParams.m_FullScreen		= m(i).GetBoolProperty("fullscreen");
			}
		//	MessageBox(HWND_DESKTOP,  "hola", "titulo",0);
		}
		else 
		{
			MessageBox(HWND_DESKTOP, "WindowSet Not Found", "WindowSet Not Found", 0);
			//printf("ERROR loading the file.");
			return 1;
		}


		m=newFile["Languages"];
		if(m.Exists())
		{
			m_sEngineParams.m_CurrLanguage=m.GetPszProperty("default");
			//MessageBox(HWND_DESKTOP, m_sLanguageParams.m_CurrLanguage.c_str(),"Current Language",  0);
			int count_aux = m.GetNumChildren();
			for (int i = 0; i < count_aux; ++i)
			{
				m_sEngineParams.m_LangXMLPathVector.push_back(m(i).GetPszProperty("pathXML"));
			}		
			
		}

		m=newFile["Fonts"];
		if(m.Exists())
		{
			m_sEngineParams.m_FontsXMLPath=m.GetPszProperty("pathXML");
		}

		m=newFile["Mouse"];
		if(m.Exists())
		{
			m_sEngineParams.m_exclusiveMouse=m.GetBoolProperty("exclusiveMouse");
		}
			

			

			
			
			//printf("Type: %s, X: %d, Y: %d, Z: %d\n", type, pos_x, pos_y, pos_z);
		

		/*for(int k=0;k<m_sLanguageParams.m_LangXMLPathVector.size();++k)
		{
			MessageBox(HWND_DESKTOP,  m_sLanguageParams.m_LangXMLPathVector[k].c_str(),        "Language info loaded",0);
		}*/
		
	}
	return 0;
}